package com.feng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feng.constants.SystemConstants;
import com.feng.domain.ResponseResult;
import com.feng.domain.vo.ArticleListPageVo;
import com.feng.domain.vo.CommentVo;
import com.feng.enums.AppHttpCodeEnum;
import com.feng.exception.SystemException;
import com.feng.mapper.CommentMapper;
import com.feng.domain.entity.Comment;
import com.feng.service.CommentService;
import com.feng.service.UserService;
import com.feng.utils.BeanCopyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 评论表(Comment)表服务实现类
 *
 * @author makejava
 * @since 2023-04-15 15:04:09
 */
@Service()
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private UserService userService;

    @Override
    public ResponseResult commentList(String commentType, Long articleId, Integer pageNum, Integer pageSize) {

        //条件 文章id相同 并且时根评论
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SystemConstants.ARTICLE_COMMENT.equals(commentType), Comment::getArticleId, articleId)
                .eq(Comment::getRootId, SystemConstants.COMMENT_ROOT) //设置查询根评论
                .eq(Comment::getType, commentType) //评论类型
                .orderByDesc(Comment::getCreateTime); //按创建时间降序排列

        Page<Comment> page = new Page<>(pageNum, pageSize);
        page(page, queryWrapper);

        //转换Vo并添加用户昵称和被回复的用户昵称
        List<CommentVo> commentVos = toCommentVoList(page.getRecords());

        //通过查询出来的根评论去查询根评论的子评论
        commentVos.stream()
                .parallel()
                .map(commentVo -> {
                    LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
                    wrapper.eq(Comment::getRootId, commentVo.getId())
                            .orderByAsc(Comment::getCreateTime);
                    List<Comment> list = list(wrapper);
                    commentVo.setChildren(toCommentVoList(list));
                    return commentVo;
                })
                .collect(Collectors.toList());

        //返回
        return ResponseResult.okResult(new ArticleListPageVo(commentVos, page.getTotal()));
    }

    @Override
    public ResponseResult commentSave(Comment comment) {
        if(!StringUtils.hasText(comment.getContent())){
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }
        save(comment);
        return ResponseResult.okResult();
    }

    //用来转换CommentVo并添加用户昵称和被回复的用户昵称
    public List<CommentVo> toCommentVoList(List<Comment> list){
        List<CommentVo> commentVos = BeanCopyUtil.articleListVo(list, CommentVo.class);
        commentVos.stream()
                .parallel()
                .map(commentVo -> {
                    String nickName = userService.getById(commentVo.getCreateBy()).getNickName();
                    commentVo.setUsername(nickName);
                    if (commentVo.getToCommentUserId() != -1) {
                        String toCommentUserName = userService.getById(commentVo.getToCommentUserId()).getNickName();
                        commentVo.setToCommentUserName(toCommentUserName);
                    }
                    String avatar = userService.getById(commentVo.getCreateBy()).getAvatar();
                    if (avatar != null){
                        commentVo.setAvatar(avatar);
                    }
                    return commentVo;
                })
                .collect(Collectors.toList());
        return commentVos;
    }
}

